package com.xkcoding.mq.kafka.handler;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.springframework.kafka.support.LoggingProducerListener;
import org.springframework.stereotype.Component;


/**
 * @Author dengkaibing
 * @Date 2022/7/18 14:18
 */
@Slf4j
@Component
public class KafkaProduceListener extends LoggingProducerListener<String, String> {


    /**
     * 消息发送成功后动作
     * @param recordMetadata the result of the successful send operation
     */
    @Override
    public void onSuccess(ProducerRecord<String, String> producerRecord, RecordMetadata recordMetadata) {
        log.info("消息发送成功");
        log.info("----topic:" + producerRecord.topic() + ", partition:" + producerRecord.partition() +
            ", key:" + producerRecord.key() + ", value:" + producerRecord.value() + ", RecordMetadata:" + recordMetadata.toString() + "----");
    }

    /**
     * 消息发送失败
     * @param producerRecord the failed record
     * @param exception the exception thrown
     */
    @Override
    public void onError(ProducerRecord<String, String> producerRecord, Exception exception) {
        log.info("消息发送失败！");
        log.info("----topic:" + producerRecord.topic() + ", partition:" + producerRecord.partition() +
            ", key:"+ producerRecord.key() + ", value:" + producerRecord.value() + ", exception:" + exception + "----");
    }
}
